$(function () {
    $("#jqGrid").jqGrid({
        url: '../employeeattendancemanagement/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
            { label: '员工姓名', name: 'empName', index: 'emp_name', width: 80 },
            { label: '员工id', name: 'empId', index: 'emp_id', width: 80,hidden:true },
            { label: '身份证', name: 'idno', index: 'idno', width: 80 ,hidden:true},
            { label: '公司', name: 'company', index: 'company', width: 100 },
            { label: '部门', name: 'dept', index: 'dept', width: 80 },
            { label: '岗位', name: 'post', index: 'post', width: 80,formatter:getDictInfo },
            { label: '年', name: 'year', index: 'year', width: 80 },
            { label: '月', name: 'mouth', index: 'mouth', width: 80 },
            { label: '出勤天数', name: 'attendanceDays', index: 'attendance_days', width: 80 },
            { label: '年休天数', name: 'annualLeaveDays', index: 'Annual_leave_days', width: 80 },
            { label: '事假天数', name: 'compassionateLeaveDays', index: 'Compassionate_leave_days', width: 80 },
            { label: '婚假天数', name: 'marriageHolidayDays', index: 'Marriage_holiday_days', width: 80 },
            { label: '病假天数', name: 'sickLeaveDays', index: 'sick_leave_days', width: 80 },
            { label: '丧假天数', name: 'funeralDays', index: 'Funeral_days', width: 80 },
            { label: '产假天数', name: 'maternityLeaveDays', index: 'maternity_leave_days', width: 80 },
            { label: '调休天数', name: 'takeOffDays', index: 'Take_off_days', width: 80 },
            { label: '录入人', name: 'cusename', index: 'cusename', width: 80 },
            { label: '录入时间', name: 'cdate', index: 'cdate',width: 100, formatter:"date", formatoptions: {newformat:'Y-m-d'}  }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        shrinkToFit:false,
        autoScroll: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    utils.popGrid();
    var r = utils.getDictList("station,training");
    vm.dictList = r.station;
});

var vm = new Vue({
	el:'#rrapp',

	data:{
        q: {
            year: null
        },
		showList: true,
        dictList:null,
		title: null,
		employeeAttendanceManagement: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.employeeAttendanceManagement = {
                empName:""
			};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.employeeAttendanceManagement.id == null ? "../employeeattendancemanagement/save" : "../employeeattendancemanagement/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.employeeAttendanceManagement),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../employeeattendancemanagement/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../employeeattendancemanagement/info/"+id, function(r){
                vm.employeeAttendanceManagement = r.employeeAttendanceManagement;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'year': vm.q.year,
                    'mouth': vm.q.mouth,'empName': vm.q.empName,'company': vm.q.company,'post': vm.q.post,
                    'dept': vm.q.dept},
                page:page
            }).trigger("reloadGrid");
		},

    /*    popUser: function(){
            popPage.popUserGrid();
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择员工",
                area: ['750px', '400px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popUser"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var grid = $("#popUserGrid");
                    var rowKey = grid.getGridParam("selrow");
                    if(!rowKey){
                        alert("请选择一条记录");
                        return ;
                    }
                    var rowData=grid.jqGrid("getRowData",rowKey);
                    vm.employeeAttendanceManagement.empName=rowData.userdesc;
                    vm.employeeAttendanceManagement.idno=rowData.idno;
                  /!*  vm.employeeAttendanceManagement.company=rowData.company;
                    vm.employeeAttendanceManagement.companyid=rowData.companyId;*!/
                    vm.employeeAttendanceManagement.dept=rowData.dept;
                    vm.employeeAttendanceManagement.departmentId=rowData.deptNo;
                    vm.employeeAttendanceManagement.empId=rowKey;


                    vm.employeeAttendanceManagement.post=rowData.post;
                    layer.close(index);
                }
            });
        },*/
        popUser: function(){
     var page = $("#popGrid").jqGrid('getGridParam','page');
     $("#popGrid").jqGrid('setGridParam',{
     postData:{
     'iswork': '1',
     'isHuashun': 'Y',
     },
     page:page
     }).trigger("reloadGrid");
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择员工",
                area: ['800px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var grid = $("#popGrid");
                    var rowKey = grid.getGridParam("selrow");
                    if(!rowKey){
                        alert("请选择一条记录");
                        return ;
                    }
                    var rowData=grid.jqGrid("getRowData",rowKey);
                    vm.employeeAttendanceManagement.empName=rowData.name;
                    vm.employeeAttendanceManagement.idno=rowData.idno;
                     vm.employeeAttendanceManagement.company=rowData.company;
                    vm.employeeAttendanceManagement.companyId=rowData.companyId;
                     vm.employeeAttendanceManagement.dept=rowData.department;
                    vm.employeeAttendanceManagement.post=rowData.post;
                    vm.employeeAttendanceManagement.empId=rowKey;

                    layer.close(index);
                }
            });
        },
	}
});